Understanding Emacs modes

noshadow

Marie-Hélène Burle

September 17, 2024


Why use Emacs?


To brag.

noshadow

Why use Emacs?


To brag. Obviously.

noshadow

Why use Emacs?


But there are other reasons:

  • Free and open source
  • Endlessly customizable
  • Amazing diff
  • Macros
  • Text searching
  • IDE
  • Lossless undo/redo
  • Fun!

Now … getting started can be daunting

… and it doesn’t necessarily get easier

But it’s all worth it!

A few Emacs concepts

Emacs Lisp

Emacs Lisp is a dialect of the Lisp programming language developed especially to write the editing functionality of the Emacs text editor (the rest of Emacs and its interpreter are written in C)

Emacs is endlessly customizable to anyone with a basic knowledge of Emacs Lisp. In particular, variables and functions setting the behaviour and appearance of the text editor can be created or modified

Graphical display

noshadow

Graphical display

noshadow

Graphical display

noshadow

Graphical display

noshadow

Graphical display

noshadow

Graphical display

noshadow

Graphical display

noshadow

Graphical display

noshadow

Graphical display

noshadow

Graphical display

noshadow

Keybindings (kbd)

Kbd notations

C-h means pressing the Control key and the H key together

M-x means pressing the Alt (Windows) or Option (MacOS) key and the X key together

C-h m means pressing the Control key and the H key together, then pressing the M key

C-c C-x m means pressing Ctl+C, then Ctl+X, then M

C-x C-c M-w C-m M-v M-t M-u means that you probably should choose another kbd

Command execution

A useful way to execute a command interactively, when it is not bound to a kbd, is to type M-x (this brings up the minibuffer, a place in which to type inputs) followed by the command name

Time to talk about Emacs modes

Major modes

Different types of text require different behaviours, syntax highlighting, formatting, functions, etc.

Each type of buffer (e.g. Python script, Markdown document, Julia REPL, Bash shell, directory editor, pdf) is associated with a major mode

File extensions, particular markers in the file, or other elements tell Emacs to automatically switch to the appropriate major mode

Only one major mode is active at a time

Switching to a different major mode is possible by running the corresponding major mode command (e.g. M-x python-mode will switch to Python mode)

Fundamental mode

fundamental-mode is the most basic major mode, with no particular feature

This is the mode enabled by default if Emacs cannot detect what specific major mode to enable

Minor modes

Minor modes provide additional and optional features that can be turned on or off (e.g. spell checking, auto-indentation, fancy undo behaviour, fancy parenthesis matching)

Minor modes can be turned on/off by running the corresponding minor mode commands (e.g. M-x flyspell-mode will turn spell checking on/off)

Many minor modes can be active at the same time

The command consult-minor-mode-menu from the package consult makes this particularly easy

List of enabled modes

By default, C-h m or M-x describe-mode will open a list and description of the active modes

The major mode can also be determined with C-h v major-mode (C-h v runs the command describe-variable)

A list of minor modes can also be viewed with C-h v minor-mode-list

Again, consult’s consult-minor-mode-menu makes all this much nicer

The mode line

Another way to get some information about enabled modes is the mode line

noshadow

Hooks

Minor modes can be automatically enabled in particular major modes with hooks

For example, to start the aggressive indent minor mode with the ESS R major mode:

(add-hook 'ess-r-mode-hook 'aggressive-indent-mode)

Or, using use-package:

(use-package aggressive-indent
:hook (ess-r-mode . aggressive-indent-mode))

Customizing modes

In Emacs, everything is customizable

To customize modes, you can write your own Emacs Lisp code in your init file (the configuration file that gets loaded when Emacs launches) or you can use the easy customization interface

For example, to customize the Python major mode, you would run M-x customize-group python

Customizing kbd

Most modes come with specific keymaps: kbd only active when the mode is enabled. These kbd can be customized

For example, to modify the kbd for the function markdown-outline-previous in the markdown-mode-map:

(define-key markdown-mode-map (kbd "M-p") 'markdown-outline-previous)

Or, using use-package:

(use-package markdown-mode
    :bind (:map markdown-mode-map
                ("M-p" . markdown-outline-previous)))

Polymode

While it is normally impossible to associate multiple major modes with a single buffer, Polymode allows to insert sections of a major mode within another major mode

This is extremely convenient for instance to embed sections of code within human text, or even to have code executed within human text (e.g. R Markdown or its successor Quarto, Org Babel)

Polymode

markdown-mode with snippets of julia-mode:

Julia has "assignment by operation" operators:

```{julia}
a = 2;
a += 7    # this is the same as a = a + 7
```

There is a *left* division operator:

```{julia}
2\8 == 8/2
```

Polymode

Rendered by Quarto into:

Confused?

I will be giving an intro Emacs course on September 26 and October 3

You can sign up for it here